package logic.utils;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;


import ventanas.prueba.PanelEstadisticas;
import ventanas.prueba.PanelInicial;
import ventanas.prueba.VentanaJuego;

import logic.blocks.Block;
import logic.characters.Player;
import logic.enemies.Enemy;
import logic.entities.Background;
import logic.pruebas.GamePanel;

/**
 * @author Unai
 * Clase que prepara la partida y la ejecuta
 *
 */
public class Game implements Runnable{
	private boolean end = false;
	public int experience;
	public int enemiesKilled;
	private PanelEstadisticas panelEstad;
	private Player player;
	private Background background;
	private GamePanel panel;
	private ArrayList<Block> blocks;
	private ArrayList<Enemy> enemies;
	private int level;
	private PanelInicial pI;
	private int idPersonaje;
	private int idPartida;
	

	/**
	 * @param player
	 * @param level
	 * @param difficulty
	 * @param window
	 */
	public Game(Player player, int level, int difficulty, VentanaJuego window, int idPersonaje, int idPartida, PanelInicial pI, GamePanel panel) {
//		GamePanel panel = new GamePanel(300);
		this.player = player;
		this.level = level;
		this.panel = panel;
		this.pI = pI;
		this.idPersonaje = idPersonaje;
		this.idPartida = idPartida;
		System.out.println("Game entered");
		player.setPanel(panel);
		panel.setLayout(null);
		window.pack();
		window.setLocationRelativeTo(null);
		window.getContentPane().add(panel, 1);
		window.setPlayer(player);
		panel.setVisible(true);
		Background background = new Background(0, 0, panel, "resources/back" + level + ".gif", player);
		this.background = background;
		ArrayList<Block> blocks = new ArrayList<Block>();
		ArrayList<Enemy> enemies = new ArrayList<Enemy>();
		this.blocks = blocks;
		this.enemies = enemies;
		panel.setPlayer(player); 

		new MapLoader(blocks, enemies, panel, player, difficulty, level, this);

		background.dibujar();

		for (Block b : blocks) {
			b.dibujar();
		}  
		player.dibujar();
		panel.updateUI();
		player.enemies = enemies;
		player.blocks = blocks;  
		panel.timer.start();
		
		panel.requestFocus();
		panel.requestFocusInWindow();
		panel.setRequestFocusEnabled(true);
		panel.grabFocus();
		
		System.out.println("Loop");


	}
	
	public void run() {
		while (!end) {
			player.update();

			for (Block b : blocks) {
				b.update(background);
				b.impact(player);
				background.updateBackground(enemies);
				for (Enemy e : enemies) {
					e.update(player);
					b.impact(e);
					e.impact(player);
				}
			}
			panel.drawTime();
			panel.updateBars();
			panel.updateUI();
			if (player.dead || panel.time < 0 || player.endReached) {
				System.out.println("if");
				end = true;

			}
		}
		endGame();
	}
	
	public void endGame() {
		panelEstad = new PanelEstadisticas(pI, panel.time,enemiesKilled, experience, level, player.dead, player.endReached, player.attack, player.defense, player.magic, player.resistance, player.speed, player.jump, player.stamina, player.life, idPersonaje, idPartida);
		pI.ventanaJuego.getContentPane().add(panelEstad, 0);
		panelEstad.setVisible(true);
	}
	
	private class TAdapter extends KeyAdapter{
		public void keyReleased(KeyEvent e){
			System.out.println("released");
			if(player!=null) {
				player.keyReleased(e);
			}
		}
		public void keyPressed(KeyEvent e){
			if(player!=null) {
				player.keyPressed(e);
			}
		}
	}
}
